<?php

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Team extends Model
{

    protected $table = 'team';
    public $timestamps = true;
    protected $dateFormat = 'U';

    static function getTeaMById($team_id){
         return self::where('id',$team_id)->value('team_name');
    }

    public function getTeamList($page,$pageSize){
         $res = DB::table('team')
             ->select('id','team_img','team_name', 'motto','tea_log')
             ->offset(($page-1)*$pageSize)
             ->limit($pageSize)
             ->get();
         foreach ($res as $k=>&$v){
             $v->team_img = json_decode($v->team_img,true);
         }
         return $res;
    }

    //球队详情
    public function getDetail($id){
      $res =  DB::table('team')->where('id',$id)->first(['team_name','motto','team_img','introduce','establish','coach_id','tea_log']) ?? [];
      if ($res){
          $res->team_img = json_decode($res->team_img);
          $res->coach = Tea::where('id',$res->coach_id)->value('user_name');
      }
      return $res;
    }
    //球队剩余课时
    public static function overTime($team_id){
        return  self::where('id',$team_id)->first(['period','team_name','team_status','tea_log']);
    }

    public static function smsUserCourse($team_id){
        if (empty($team_id)){
            return false;
        }
        $user_id = User::whereIn('team_id',$team_id)->pluck('id')->toArray();
        if (empty($user_id)){
            return false;
        }
        //查询请假的人
        $cause_id = Cause::where('start_date','<',time())->where('end_date','>',time())->pluck('user_id')->toArray();
        //查询休课的uid
        $lev_uid = User::where('leastatus',1)->pluck('id')->toArray();
        //查询课程过期的或者课程为0的
        $period_uid = User::where('period',0)->orwhere('class_end_time','>',time())->pluck('id')->toArray();
        $heBing = array_merge($cause_id,$lev_uid,$period_uid);
        //找出交集
        $new_user = array_intersect($user_id,$heBing);
        //请假的user_id解决
        $ids = array_diff($user_id,$new_user);
        //对这些id进行发送短信
        $sms_ids = [];
        foreach ($ids as $k=>$v){
            $sms_ids[]=  $v;
        }
        return $sms_ids;
    }



}
